/* 
 * File:   Lesson.cpp
 * Author: Tony
 * 
 * Created on February 8, 2013, 2:40 PM
 */

#include "Lesson.h"

using namespace std;

//check and return all the lesson that the lecturer has
vector<Lesson> Lesson::returnLesson(string lectID)
{
    vector<Lesson> tempLesson;
    //loop thru all the lessons
    for (int i = 0; i < allLesson.size(); i++)
    {
        //the lesson is taught by the lecturer
        if(allLesson[i].lecturerID == lectID )
        {
            tempLesson.push_back(allLesson[i]);
        }
    }
    return tempLesson;
}

//check and return the list of student of a particular lesson
vector<student> Lesson::returnListOfStudents(string lessonID)
{
    //loop thru all the lessons
    for (int i = 0; i < allLesson.size(); i++)
    {
        if(allLesson[i].lessionID == lessonID )
        {
            return allLesson[i].studentsOfLesson;
        }
    }
}

//display the list of absentees for particular lesson
void Lesson::printAttendance(string lessonID)
{
    //loop thru all the lessons
    for (int i = 0; i < allLesson.size(); i++)
    {
        if(allLesson[i].lessionID == lessonID )
        {
            //loop thru the lesson's list of absentees
            for (int j = 0; i < allLesson[i].listOfAbsent.size(); i++)
            {
                cout << allLesson[i].listOfAbsent[j].studentID << " " << 
                        allLesson[i].listOfAbsent[j].studentName << endl;
            }
            break;
        }
    }
}

//add student to current lesson
bool Lesson::addStudentToLesson(string lessonID, string name, string id)
{
    student temp;
    temp.studentID = id;
    temp.studentName = name;
    
    //loop thru all the lessons
    for (int i = 0; i < allLesson.size(); i++)
    {
        if(allLesson[i].lessionID == lessonID )
        {
            //loop thru the list of students of the lesson
            for (int j = 0; i < allLesson[i].studentsOfLesson.size(); i++)
            {
                if (allLesson[i].studentsOfLesson[j].studentID == id && 
                        allLesson[i].studentsOfLesson[j].studentName == name)
                {
                    //student already exist
                    return false;
                }
            }
            //student does not exist, added to the lesson
            allLesson[i].studentsOfLesson.push_back(temp);
            return true;
        }
    }
}

//remove student from the list of the lesson
bool Lesson::removeStudentFromLesson(string lessonID, string name, string id)
{
    //loop thru all the lessons
    for (int i = 0; i < allLesson.size(); i++)
    {
        if(allLesson[i].lessionID == lessonID )
        {
            //loop thru the list of students of the lesson
            for (int j = 0; i < allLesson[i].studentsOfLesson.size(); i++)
            {
                if (allLesson[i].studentsOfLesson[j].studentID == id && 
                        allLesson[i].studentsOfLesson[j].studentName == name)
                {
                    //student exist, remove from list
                    allLesson[i].studentsOfLesson.erase(studentsOfLesson.begin() + j);
                    return true;
                }
            }
            //student does not exist 
            return false;
        }
    }
}

//let the student check those lessons that they are absent from
vector<Lesson> Lesson::checkAbsentRecord(string id)
{
    vector<Lesson> temp;
    
    //loop thru all the lessons
    for (int i = 0; i < allLesson.size(); i++)
    {
        //loop thru the list of absentee for the lesson
        for (int j = 0; j < allLesson[i].listOfAbsent.size(); j++)
        {
            if (allLesson[i].listOfAbsent[j].studentID == id)
            {
                temp.push_back(allLesson[i]);
            }
        }
    }
    return temp;
}

//let the student check those classes that he has
vector<Lesson> Lesson::checkLessonInfo(string id)
{
    vector<Lesson> temp;
    
    //loop thru all the lessons
    for (int i = 0; i < allLesson.size(); i++)
    {
        //loop thru the list of students for the lesson
        for (int j = 0; j < allLesson[i].studentsOfLesson.size(); j++)
        {
            if (allLesson[i].listOfAbsent[j].studentID == id)
            {
                temp.push_back(allLesson[i]);
            }
        }
    }
    return temp;
}

//create new module
bool Lesson::makeNewModule(string subCode, string subName, int noOfClass)
{
    Modules temp;
    
    //loop thru all the modules record
    for (int i = 0; i < allModules.size(); i++)
    {
        //check if the subject code exist
        if (allModules[i].subjectCode == subCode)
        {
            return false;
        }
    }
    //add in the module
    temp.subjectCode = subCode;
    temp.subjectName = subName;
    temp.totalNumOfClass = noOfClass;
    allModules.push_back(temp);
    return true;
}

//change the module code of existing module
bool Lesson::editModuleCode(string subCode, string newSubCode)
{
    //loop thru all the modules record
    for (int i = 0; i < allModules.size(); i++)
    {
        //check if the subject code exist
        if (allModules[i].subjectCode == subCode)
        {
            //loop thru all the modules record check if new code exists
            for (int j = 0; j < allModules.size(); i++)
            {
                if (allModules[j].subjectCode == newSubCode)
                {
                    return false;
                }
                
            }
            allModules[i].subjectCode == newSubCode;
            return true;
        }
    }
    //module not found
    return false;
}

//change the module name of existing module
bool Lesson::editModuleName(string subCode, string subName)
{
    //loop thru all the modules record
    for (int i = 0; i < allModules.size(); i++)
    {
        //check if the subject code exist
        if (allModules[i].subjectCode == subCode)
        {
            allModules[i].subjectName == subName;
            return true;
        }
    }
    //module not found
    return false;
}

//change the module total number of lesson of existing module
bool Lesson::editModuleTotalLesson(string subCode, int noOfLesson)
{
    //loop thru all the modules record
    for (int i = 0; i < allModules.size(); i++)
    {
        //check if the subject code exist
        if (allModules[i].subjectCode == subCode)
        {
            allModules[i].totalNumOfClass == noOfLesson;
            return true;
        }
    }
    //module not found
    return false;
}

//delete an existing module
bool Lesson::deleteModule(string subCode)
{
    //loop thru all the modules record
    for (int i = 0; i < allModules.size(); i++)
    {
        //check if the subject code exist
        if (allModules[i].subjectCode == subCode)
        {
            allModules.erase(allModules.begin() + i);
            return true;
        }
    }
    //module not found
    return false;
}

bool Lesson::markAttendance(string lessonID, student absStudent)
{
    //loop thru all the lesson record
    for (int i = 0; i < allLesson.size(); i++)
    {
        //check if the lessson exist
        if (allLesson[i].lessionID == lessonID)
        {
            //loop thru the list of absentees
            for(int j = 0; j < allLesson[i].listOfAbsent.size(); j++)
            {
                //check if the student already in the list
                if (allLesson[i].listOfAbsent[j].studentID == absStudent.studentID)
                {
                    return false;
                }
            }
            //add the student to list
            allLesson[i].listOfAbsent.push_back(absStudent);
            return true;
        }
    }
    //lesson not found
    return false;
}

bool Lesson::removeFromAbsent(string lessonID, string studID)
{
    //loop thru all the lesson record
    for (int i = 0; i < allLesson.size(); i++)
    {
        //check if the lesson exist
        if (allLesson[i].lessionID == lessonID)
        {
            //loop thru the list of absentees
            for(int j = 0; j < allLesson[i].listOfAbsent.size(); j++)
            {
                //check if the student in the list
                if (allLesson[i].listOfAbsent[j].studentID == studID)
                {
                    //remove the student from list
                    allLesson[i].listOfAbsent.erase(allLesson[i].listOfAbsent.begin() + j);
                    return true;
                }
            }
            return false;
        }
    }
    //module not found
    return false;
}

bool Lesson::bookVenue(string lessonID, string venue, date_t date)
{
    for (int i = 0; i < allLesson.size(); i++)
    {
        //check if the lesson exist
        if (allLesson[i].lessionID == lessonID)
        {
            for (int j = 0; j < allLesson.size(); j++)
            {
                if (allLesson[j].venue == venue && allLesson[j].dateTime.day == date.day
                        && allLesson[j].dateTime.month == date.month
                        && allLesson[j].dateTime.year == date.year)
                {
                    return false;
                }
            }
            allLesson[i].venue = venue;
            allLesson[i].dateTime = date;
            return true;
        }
    }
    return false;
}